package com.wolfking.converter.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Sets;
import com.wolfking.converter.entity.SqlDefine;
import com.wolfking.converter.event.SqlDefineEvent;
import com.wolfking.converter.service.SqlDataSourceService;
import com.wolfking.converter.service.SqlDefineService;
import com.wolfking.jeesite.config.SsoConfig;
import com.wolfking.jeesite.entity.PageInfo;
import com.wolfking.jeesite.sign.Sign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.net.URLEncoder;
import java.util.Objects;

@Slf4j
@Controller
@RequestMapping("/sql/define")
public class SqlDefineController {

    @Autowired
    private SqlDefineService defineService;

    @Autowired
    private SqlDataSourceService sqlDataSourceService;

    @Autowired
    private SsoConfig ssoConfig;

    @Sign(code = "sql:define:view")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public String list(Model model, SqlDefine sqlDefine, @RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "30") int pageSize) {
        QueryWrapper<SqlDefine> sqlDefineQueryWrapper = defineService.queryWrapperConfig(sqlDefine,
                Sets.newHashSet("dataSource"), Sets.newHashSet("sqlDescribe"));
        sqlDefineQueryWrapper.orderByDesc("update_time");
        PageInfo<SqlDefine> pageInfo = defineService.pageQuery(sqlDefineQueryWrapper, pageNum, pageSize);
        model.addAttribute("define", sqlDefine);
        model.addAttribute("page", pageInfo);
        model.addAttribute("datasourceList", sqlDataSourceService.list());
        return "sql/define/list";
    }


    @Sign(code = "sql:define:view")
    @RequestMapping(value = "/edit", method = {RequestMethod.GET})
    public String editGet(Model model, @RequestParam(name = "id", required = false) Integer id) {
        if (Objects.nonNull(id)) {
            SqlDefine byId = defineService.getById(id);
            if (Objects.isNull(byId)) {
                byId = new SqlDefine();
            }
            model.addAttribute("define", byId);
        } else {
            model.addAttribute("define", new SqlDefine());
        }
        model.addAttribute("datasourceList", sqlDataSourceService.list());
        return "sql/define/edit";
    }

    @Sign(code = "sql:define:edit",event = SqlDefineEvent.class)
    @RequestMapping(value = "/edit", method = {RequestMethod.POST})
    public String editPost(RedirectAttributes redirectAttributes, SqlDefine data) throws Exception {
        if (Objects.isNull(data.getId())) {
            defineService.save(data);
            redirectAttributes.addFlashAttribute("message", "添加SQL成功");
        } else {
            redirectAttributes.addFlashAttribute("message", "修改SQL成功");
            defineService.updateById(data);
        }
        return ssoConfig.getRedirectWithPath(String.format("/sql/define/list?dataSource=%s&sqlDescribe=%s",
                data.getDataSource(), URLEncoder.encode(data.getSqlDescribe(), "UTF-8")));
    }

    @Sign(code = "sql:define:edit")
    @RequestMapping(value = "/delete", method = {RequestMethod.GET})
    public String delete(RedirectAttributes redirectAttributes, @RequestParam("id") Integer id) {
        SqlDefine sqlDefine = defineService.getById(id);
        if (sqlDefine == null) {
            redirectAttributes.addFlashAttribute("messageType", "danger");
            redirectAttributes.addFlashAttribute("message", "数据不存在");
        } else {
            defineService.deleteById(id);
            redirectAttributes.addFlashAttribute("message", "删除SQL成功");
        }
        if (sqlDefine == null) {
            return ssoConfig.getRedirectWithPath("/sql/define/list");
        } else {
            return ssoConfig.getRedirectWithPath("/sql/define/list?dataSource=" + sqlDefine.getDataSource());
        }
    }
}
